package com.niit.listener;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Timestamp;
import java.util.Date;

import javax.swing.JOptionPane;

import com.niit.dao.BookDao;
import com.niit.dao.impl.BookDaoImpl;
import com.niit.entity.Book;
import com.niit.view.AddBookView;
import com.niit.view.MainView;

/**
 * 保存书籍
 *
 */
public class SaveBookListener extends MyMouseListenerAdapter {

	private BookDao bookDao;

	public SaveBookListener() {
		this.bookDao = new BookDaoImpl();
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		String bookName = view.getBookNameTextField().getText();
		String bookPrice = view.getBookPriceTextField().getText();
		String bookAuthor = view.getBookAuthorTextField().getText();
		String bookPublisher = view.getBookPublisherTextField().getText();
		Book book = new Book(Double.parseDouble(bookPrice), bookName, bookAuthor, bookPublisher);
		book.setCreateTime(new Timestamp(System.currentTimeMillis()));
		boolean flag = bookDao.insertBook(book);
		if (flag) {
			JOptionPane.showMessageDialog(null, "添加成功");
		} else {
			JOptionPane.showMessageDialog(null, "添加失败");
		}
		new MainView(null);
		view.dispose();
	}

}
